<?php

/*
 * 这不是一个开放软件，您不能用于商业应用
 * 不允许对程序代码以任何形式任何目的的再发布。
 * Copyright (c) 2015 YOUM.Co.Ltd. All rights reserved..
 * 网站地址：http://www.huishuojs.com
 * Author Zhyu
 */

/**
 * @author Zhyu
 * @date 2015-12-20 02:21:04
 * @copyright 杭州晖硕有码科技
 */
class Fora extends PC_Controller{
    //put your code here
    protected $go_url = '/admin/fora/fora_index';
    public function __construct(){
        parent::__construct();
        $this->load->model('fora_model');
        $this->load->model('fora_log_model');
        ini_set('max_execution_time', '0');
        ini_set('memory_limit','512M');
    }
    /**
     * 列表
     */
    public function fora_index(){
        $this->load->view('/admin/fora_list.php',  $this->data);
    }
    /**
     *
     */
    public function ajax_fora_list(){
        $data = $_GET;
        $start = $data['iDisplayStart']; //显示的起始索引
        $length = $data['iDisplayLength'];//显示的行数
        $sort_th = $data['mDataProp_'.$data['iSortCol_0'].''];//被排序的列
        $sort_type = $data['sSortDir_0'];//排序的方向 "desc" 或者 "asc".
        $where=array();
        /*$search = $data ['sSearch'];//全局搜索字段
        $data['username'] ? $where['username like'] = '%'.trim($data['username']).'%' :'';
        $data['phone'] ? $where['phone like'] = '%'.trim($data['phone']).'%' :'';
        $data['parent_id']?$where['parent_id']=$data['parent_id']:'';
        $where['concat(id) like'] = '%'.trim($search).'%';*/
        $this->data['count'] = $this->fora_model->getCount($where);
        $aaData = array();
        if($this->data['count']) {
            $order_by = $sort_th." ".$sort_type;
            $fora_list = $this->fora_model->getForaList($where,$length,$start,$order_by);
            //echo $this->db->last_query();
            foreach($fora_list as $key => &$value) {
                $value['create_time']=date('Y-m-d',$value['create_time']);
                $value['update_time']=date('Y-m-d',$value['update_time']);
                switch ($value['status']){
                    case 1:
                        $value['status']='未售';
                        break;
                    case 2:
                        $value['status']='已售';
                        break;
                    case 3:
                        $value['status']='锁定';
                        break;
                }
                $query_url= '<a href="/admin/fora/fora_log/'.$value['id'].'"><span class="btn btn-xs btn-primary">查看供位明细</span></a>';
                $edit_url = '<a href="/admin/fora/edit_page/'.$value['id'].'"><span class="btn btn-xs btn-primary">编辑</span></a>';
                $fora_list[$key]['operate'] = $query_url.' '.$edit_url;
            }
            $aaData = $fora_list;

        }
        $output['aaData'] = $aaData;
        $output['sEcho'] = $_GET['sEcho'];
        $output['iTotalDisplayRecords'] =  $this->data['count']; //总共有几条数据
        $output['iTotalRecords'] = $this->data['count']; //总共有几条数据
        echo json_encode($output); //最后把数据以json格式返回
    }
    /**
     * @brief 进入编辑页面
     * @param Null
     * @par 2015/10/12 Ver 1.00 Created by Allen
     */
    public function edit_page() {
        $id = $this->uri->segment(4);
        $where['id'] = $id;
        $fora_info = $this->fora_model->checkFora($where);

        $this->data['data'] = $fora_info;
        $this->load->view('/admin/fora_edit',$this->data);
    }
    /**
     * @brief 保存编辑信息
     * @param Null
     * @par 2015/10/12 Ver 1.00 Created by Allen
     */
    public function edit() {
        $data = $_POST;
        $where['id'] = $data['id'];
        $edit_result=false;
        if(isset($where['id']) && $where['id']){
            $fora_info=$this->fora_model->checkFora($where);
			$this->load->model('user_model');
            if(isset($fora_info) && $fora_info){
                if($fora_info['num'] < $data['num']){
                    $num=$data['num']-$fora_info['num'];
                    if(isset($num) && $num>0){
                        for($i=0;$i<$num;$i++){
                            $fora_log=array();
                            $fora_log['code']=$data['code'].($fora_info['num']+$i+1);
							$fora_log['status']=$data['status'];
                            $is_set=$this->fora_log_model->getCount(array('code'=> $fora_log['code']));
                            if($is_set >0){
                                continue;
                            }
							$is_use=$this->user_model->getCount(array('csg'=> $fora_log['code']));
							if($is_use >0){
								 $fora_log['status']=2;
							}
                          
                            $fora_log['fora_id']=$data['id'];
                            $fora_log['create_time']=time();
                            $fora_log['update_time']=time();
                            $this->fora_log_model->addForaLog($fora_log);
                        }
                    }
                }else{
                    $this->location_href($this->go_url."/3");
                }
            }
            $data['update_time']=time();
            $edit_result = $this->fora_model->editFora($where,$data);
        }
        if($edit_result) {
            $this->location_href($this->go_url."/2");
        }else {
            $this->location_href($this->go_url."/3");
        }
    }

    /**
     * @brief 删除会员信息
     * @param Null
     * @par 2015/10/12 Ver 1.00 Created by Allen
     */
    public function del() {
        $id = $this->uri->segment(4);
        $where['id'] = $id;
        $del_result = $this->fora_model->deleteUserLevel($where);
        $go_url = '/admin/user/user_list';
        if($del_result) {
            $this->location_href($this->go_url."/2");
        }else {
            $this->location_href($this->go_url."/3");
        }
    }
    /**
     * @brief 检查管理员姓名唯一性
     * @param Null
     * @par 2015/12/03 Ver 1.00 Created by Allen
     */
    public function check_sole() {
        $data = $_POST;
        $where=array();
        $sole=array();
        if(isset($data['id'])) {
            $where['id !='] = $data['id'];
        }
        if(isset($data['key']) && $data['key']){
            $where[$data['key']]=$data['value'];
        }
        if(!empty($where)){
            $sole = $this->fora_model->checkFora($where);
        }

        if($sole) {
            $message = false;
        }else {
            $message = true;
        }
        echo json_encode($message);
    }
    /**
     * @brief 进入添加页面
     * @param Null
     * @par 2015/12/03 Ver 1.00 Created by Allen
     */
    public function add_page() {
        $this->load->view('/admin/fora_add',$this->data);
    }

    /**
     * @brief 保存添加信息
     * @param Null
     * @par 2015/12/03 Ver 1.00 Created by Allen
     */
    public function add() {
        $data = $_POST;
        $data['create_time']=time();
        $data['update_time']=time();
        $add_result = $this->fora_model->addFora($data);
        if($add_result) {
            if(isset($data['num']) && $data['num']>0){
                for($i=0;$i<$data['num'];$i++){
                    $fora_log=array();
                    $fora_log['code']=$data['code'].($i+1) ;
                    $is_set=$this->fora_log_model->getCount(array('code'=> $fora_log['code']));
                    if($is_set >0){
                        continue;
                    }
                    $fora_log['status']=$data['status'];
                    $fora_log['fora_id']=$add_result;
                    $fora_log['create_time']=time();
                    $fora_log['update_time']=time();
                    $this->fora_log_model->addForaLog($fora_log);
                }
            }
            $this->location_href($this->go_url."/4");
        }else {
            $this->location_href($this->go_url."/5");
        }
    }
    public function fora_log(){
        $id = $this->uri->segment(4);
        if(!isset($id)){
            $id=0;
        }
		$this->data['username'] =  empty($_POST) ? '' : $_POST['username'];
		$this->data['code'] =  empty($_POST['code']) ? '' : $_POST['code'];
        $this->data['start_time']= empty($_POST['start_time'])?'':$_POST['start_time'];
        $this->data['end_time']= empty($_POST['end_time'])?'':$_POST['end_time'];
        $this->data['fora_id']=$id;
        $this->load->view('/admin/fora_log_list.php',  $this->data);
    }

    public function ajax_fora_lof_list(){
        $data = $_GET;
        $start = $data['iDisplayStart']; //显示的起始索引
        $length = $data['iDisplayLength'];//显示的行数
        $sort_th = $data['mDataProp_'.$data['iSortCol_0'].''];//被排序的列
        $sort_type = $data['sSortDir_0'];//排序的方向 "desc" 或者 "asc".
        $where=array();
		isset($data['fora_id'])&&$data['fora_id'] ?$where['fora_id']=$data['fora_id']:'';
		$data['start_time']?$where['use_time >=']=strtotime($data['start_time']):'';
        $data['end_time']?$where['use_time <=']=strtotime($data['end_time']):'';
        $data['username'] ? $where['username like'] = '%'.trim($data['username']).'%' :'';

        $data['start_time']?$where['status']=2:'';
        $data['end_time']?$where['status']=2:'';
        $data['username'] ? $where['status'] =2 :'';
		$data['code'] ? $where['code'] =$data['code'] :'';
		
	   /*$search = $data ['sSearch'];//全局搜索字段
        $data['username'] ? $where['username like'] = '%'.trim($data['username']).'%' :'';
        $data['phone'] ? $where['phone like'] = '%'.trim($data['phone']).'%' :'';
        $data['parent_id']?$where['parent_id']=$data['parent_id']:'';
        $where['concat(id) like'] = '%'.trim($search).'%';*/
        $this->data['count'] = $this->fora_log_model->getCount($where);
        $aaData = array();
        if($this->data['count']) {
            $this->load->model('user_model');
           // $fora_info=$this->fora_model->checkFora(array('id'=>$data['fora_id']));
            $order_by = $sort_th." ".$sort_type;
            $fora_list = $this->fora_log_model->getForaLogList($where,$length,$start,$order_by);
            //echo $this->db->last_query();
            $fora_info=array();
            foreach($fora_list as $key => &$value) {
                if(!isset($fora_info[$value['fora_id']])){
                    $fora=$this->fora_model->checkFora(array('id'=>$value['fora_id']));
                    $fora_info[$value['fora_id']]=$fora;
                }
                $value['name']=$fora_info[$value['fora_id']]['name'];
                $value['fora_code']=$fora_info[$value['fora_id']]['code'];
                $fora_info=$this->fora_model->checkFora(array('id'=>$data['fora_id']));



                $value['create_time']=date('Y-m-d',$value['create_time']);
                $value['update_time']=date('Y-m-d',$value['update_time']);
				$value['use_time']=date('Y-m-d',$value['use_time']);
                if($value['user_id'] >0){
                    /*$user=$this->user_model->checkUser(array('id'=>$value['user_id']));
                    if(isset($user['id']) && $user['id']){
                        $value['username']=$user['username'];
                    }*/
                }else{
                    $value['username']='未使用';
                }
                switch ($value['status']){
                    case 1:
                        $value['status']='未售';
                        break;
                    case 2:
                        $value['status']='已售';
                        break;
                    case 3:
                        $value['status']='锁定';
                        break;
                }
                $query_url= '';
                $edit_url = '<a href="/admin/fora/fora_log_edit_page/'.$value['id'].'"><span class="btn btn-xs btn-primary">编辑</span></a>';
                $fora_list[$key]['operate'] = $query_url.' '.$edit_url;
            }
            $aaData = $fora_list;

        }
        $output['aaData'] = $aaData;
        $output['sEcho'] = $_GET['sEcho'];
        $output['iTotalDisplayRecords'] =  $this->data['count']; //总共有几条数据
        $output['iTotalRecords'] = $this->data['count']; //总共有几条数据
        echo json_encode($output); //最后把数据以json格式返回
    }

    public function fora_log_edit_page()
    {
        $id = $this->uri->segment(4);
        $where['fora_log.id'] = $id;
        $fora_log_info = $this->fora_log_model->checkForaLogJoin($where);
        $this->data['data'] = $fora_log_info;
        $this->load->view('/admin/fora_log_edit',$this->data);
    }

public function fora_log_edit(){
    $data=$_POST;
    if(isset($data['id']) && $data['id']){
        $where['id']=$data['id'];
        $this->fora_log_model->editForaLog($where,array('status'=>$data['status'],'update_time'=>time()));
        $this->location_href("/admin/fora/fora_log/".$data['fora_id']);
    }
}
    /**
     * @brief 检查唯一性
     * @param Null
     * @par 2015/12/03 Ver 1.00 Created by Allen
     */
    public function check_log_sole() {
        $data = $_POST;
        $where=array();
        $sole=array();
        if(isset($data['id'])) {
            $where['id !='] = $data['id'];
        }
        if(isset($data['key']) && $data['key']){
            $where[$data['key']]=$data['value'];
        }
        if(!empty($where)){
            $sole = $this->fora_log_model->checkFora($where);
        }

        if($sole) {
            $message = false;
        }else {
            $message = true;
        }
        echo json_encode($message);
    }
    //根据供区名字，供区编号查找
    public function get_fora_log(){
        $data=$_POST;
        $where='fora.status=1 and fora_log.status =1';


        if(isset($data['keyword']) && $data['keyword']){
//            $where.=" and (fora.name like '%".$data['keyword']."%'";
//            $where.=" or fora.code like '%".$data['keyword']."%' or fora_log.code like '%".$data['keyword']."%')";
            $where.=" and fora_log.code like '%".$data['keyword']."%'";
        }
        if(isset($data['fora_id']) && $data['fora_id']){
            $where.=" and fora_log.fora_id= ".$data['fora_id'];

        }
        $sql="select fora_log.*,fora.name as fora_name,fora.code as fora_code,fora.status as fora_status ,fora.num as fora_num from fora_log  LEFT JOIN   fora ON  fora_log.fora_id = fora.id where  ".$where;
        $query=$this->db->query($sql);
        $fora_logs = array ();
        foreach ( $query->result () as $row ) {
            $fora_logs [] = $row;
        }
         echo json_encode($fora_logs);
    }
    //检查code是否存在和被使用
    public function check_fora_log(){
        $data=$_POST;
        $fora_log=$this->fora_log_model->checkForaLog(array('code'=>$data['csg'],'status'=>'1'));
        if(isset($fora_log) && !empty($fora_log) && isset($fora_log['id']) && $fora_log['id']){
            $message=true;
        }else{
            $message=false;
        }
        echo json_encode($message);
    }
}